﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZCrawler;

namespace ZCrawler.Unit.Test
{
    public class SogoCwbCrawlTask
    {
        public void Process()
        {
            ZTask task = new ZTask()
            {
                Url = "http://www.sogo.com.hk/cwb/index.php?option=com_shoppingdirectory&view=shopname&id=6&Itemid=23&lang=en&limitstart=0"
            };
            ZRuleXPath pageRule = new ZRuleXPath(ZXPathType.OuterHtml, "next");
            pageRule.AddXPath("//a[text()='Next']/@href");
            ZAnalysis p_analyst = new ZAnalysis(pageRule);
            p_analyst.IsPageUrlAnlysis = true;
            task.pageSwitchAnalyst = p_analyst;

            ZRuleNav navRule = new ZRuleNav("nav")
            {
                navMark = "/cwb/index.php?option=com_shoppingdirectory&amp;view=shopdetail&amp;id="
            };
            ZAnalysis n_analyst = new ZAnalysis(navRule);
            task.navAnalyst = n_analyst;

            ZRuleXPath rule_title = new ZRuleXPath(ZXPathType.InnerText, "store_title");
            rule_title.AddXPath("//*[@id=\"shoplisting\"]/table/tr[2]/td[2]/table/tr[1]/td[3]");

            ZRuleXPath rule_floor = new ZRuleXPath(ZXPathType.InnerText, "store_floor");
            rule_floor.AddXPath("//*[@id=\"shoplisting\"]/table/tr[2]/td[2]/table/tr[2]/td[3]");

            ZRuleXPath rule_location_no = new ZRuleXPath(ZXPathType.InnerText, "store_location_no");
            rule_location_no.AddXPath("//*[@id=\"shoplisting\"]/table/tr[2]/td[2]/table/tr[3]/td[3]");

            ZRuleXPath rule_categories = new ZRuleXPath(ZXPathType.InnerText, "store_categories");
            rule_categories.AddXPath("//*[@id=\"shoplisting\"]/table/tr[2]/td[2]/table/tr[4]/td[3]");

            ZRuleXPath rule_tel = new ZRuleXPath(ZXPathType.InnerText, "store_tel");
            rule_tel.AddXPath("//*[@id=\"shoplisting\"]/table/tr[2]/td[2]/table/tr[5]/td[3]");
            List<ZBasicRule> rules = new List<ZBasicRule>();
            rules.Add(rule_title);
            rules.Add(rule_floor);
            rules.Add(rule_location_no);
            rules.Add(rule_categories);
            rules.Add(rule_tel);
            ZAnalysis analysis = new ZAnalysis(rules);
            task.analysis = analysis;
            task.Launch();
        }
    }
}
